from celery import Celery
from celery_tasks import celeryconfig
from django.utils import timezone

import os
# 加载django项目配置
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hippo_api.settings.dev')

# 创建celery app
app = Celery('celery_tasks')
# 从单独的配置模块中加载配置
app.config_from_object(celeryconfig)
# 设置app自动加载任务
app.autodiscover_tasks([
    'celery_tasks',
])

# 解决时区问题,定时任务启动就循环输出
# app.now = timezone.now

# 启动celery
# celery -A celery_tasks worker -l info
# celery -A [project-name] worker --loglevel=info
# 定时任务
# celery -A celery_tasks beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler


# # 注册时间间隔对象
# from django_celery_beat.models import PeriodicTask, IntervalSchedule
# # executes every 10 seconds.
# # 创建10秒的计时器对象
# schedule, created = IntervalSchedule.objects.get_or_create(
#   		every=10,
#   		period=IntervalSchedule.SECONDS,
# )
# 添加定时任务
# period_obj = PeriodicTask.objects.create(
#      interval=schedule,   #  这就是上面我们创建的10秒周期对象
#      name='test',
#      task='celery_tasks.tasks.my_task1',
#      args=json.dumps(['1', '2', '3']),
#      expires=datetime.utcnow() + timedelta(seconds=30)
# )
